/*
 * Filename:    AuditDocument.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014-12-10
 */
package com.paic.mhis.hcpms.audit.biz.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.paic.mhis.hcpms.audit.constants.AuditConstants;
import com.paic.mhis.hcpms.itextpdf.web.pdfmodel.DocumentModel;
import com.paic.mhis.hcpms.itextpdf.web.util.HcpmsFontUtil;
import com.paic.mhis.hcpms.itextpdf.web.util.HcpmsParagraphUtil;
import com.paic.mhis.hcpms.itextpdf.web.util.HcpmsPdfCellUtil;
import com.paic.mhis.hcpms.itextpdf.web.util.HcpmsPdfTableUtil;
import com.paic.mhis.hcpms.itextpdf.web.util.HcpmsRectangleUtil;

public class AuditReportDocument implements DocumentModel {

	@Override
	public void createDocument(Document document) {
		document.setPageSize(HcpmsRectangleUtil.createNoticeRect());
	}

	@Override
	public void createContent(Document document, Map<String, Object> map) {

		Font font = HcpmsFontUtil.createTitleFont(12, Font.BOLD);
		Font fontText = HcpmsFontUtil.createTextFont(10, Font.NORMAL);

		try {
			float[] widths = { 0.15f, 0.3f, 0.15f, 0.4f };
			PdfPTable table = HcpmsPdfTableUtil.createTable(widths, 10, 90,
					Element.ALIGN_CENTER);

			table.addCell(HcpmsPdfCellUtil.createCellCol(
					HcpmsParagraphUtil.createParagraphText(font,
							getValue(map, AuditConstants.title)), new int[] {
							5, 5, 5, 5 }, Element.ALIGN_CENTER, 4,
					Rectangle.NO_BORDER, 30f));
			// 1
			table.addCell(HcpmsPdfCellUtil.createCell(
					HcpmsParagraphUtil.createParagraphText(fontText, "稽核日期:  "),
					new int[] { 4, 4, 4, 1 }, Element.ALIGN_RIGHT,
					Rectangle.NO_BORDER));

			table.addCell(HcpmsPdfCellUtil.createCell(
					HcpmsParagraphUtil.createParagraphText(fontText,
							getValue(map, AuditConstants.auditDate)),
					new int[] { 4, 4, 4, 1 }, Element.ALIGN_LEFT,
					Rectangle.NO_BORDER));

			table.addCell(HcpmsPdfCellUtil.createCell(
					HcpmsParagraphUtil.createParagraphText(fontText, "稽核时段:  "),
					new int[] { 4, 4, 4, 1 }, Element.ALIGN_RIGHT,
					Rectangle.NO_BORDER));

			table.addCell(HcpmsPdfCellUtil.createCell(
					HcpmsParagraphUtil.createParagraphText(fontText,
							getValue(map, AuditConstants.auditRangDate)),
					new int[] { 4, 4, 4, 1 }, Element.ALIGN_LEFT,
					Rectangle.NO_BORDER));

			// 5
			table.addCell(HcpmsPdfCellUtil.createCell(
					HcpmsParagraphUtil.createParagraphText(fontText, "稽核摘要:  "),
					new int[] { 4, 4, 4, 1 }, Element.ALIGN_RIGHT,
					Rectangle.NO_BORDER));
			table.addCell(HcpmsPdfCellUtil.createCellCol(
					HcpmsParagraphUtil.createParagraphText(fontText, ""),
					new int[] { 4, 4, 4, 1 }, Element.ALIGN_LEFT, 3,
					Rectangle.NO_BORDER));

			table.addCell(HcpmsPdfCellUtil.createCellCol(
					HcpmsParagraphUtil.createParagraphText(fontText,
							getValue(map, AuditConstants.auditSummary)),
					new int[] { 1, 10, 4, 40 }, Element.ALIGN_LEFT, 4,
					Rectangle.NO_BORDER));

			// 6
			table.addCell(HcpmsPdfCellUtil.createCell(
					HcpmsParagraphUtil.createParagraphText(fontText, "处理意见:  "),
					new int[] { 4, 4, 4, 1 }, Element.ALIGN_RIGHT,
					Rectangle.NO_BORDER));
			table.addCell(HcpmsPdfCellUtil.createCellCol(
					HcpmsParagraphUtil.createParagraphText(fontText, ""),
					new int[] { 4, 4, 4, 1 }, Element.ALIGN_LEFT, 3,
					Rectangle.NO_BORDER));

			table.addCell(HcpmsPdfCellUtil.createCellCol(
					HcpmsParagraphUtil.createParagraphText(fontText,
							getValue(map, AuditConstants.opinionDesc)),
					new int[] { 1, 10, 4, 40 }, Element.ALIGN_LEFT, 4,
					Rectangle.NO_BORDER));

			// 8
			table.addCell(HcpmsPdfCellUtil.createCellCol(
					HcpmsParagraphUtil.createParagraphText(fontText, ""),
					new int[] { 4, 4, 4, 1 }, Element.ALIGN_RIGHT, 3,
					Rectangle.NO_BORDER));
			table.addCell(HcpmsPdfCellUtil.createCell(HcpmsParagraphUtil
					.createParagraphText(fontText, "厦门市社会保险管理中心"), new int[] {
					4, 4, 4, 40 }, Element.ALIGN_RIGHT, Rectangle.NO_BORDER));

			// 9
			table.addCell(HcpmsPdfCellUtil.createCellCol(
					HcpmsParagraphUtil.createParagraphText(fontText, ""),
					new int[] { 4, 4, 4, 1 }, Element.ALIGN_RIGHT, 3,
					Rectangle.NO_BORDER));
			table.addCell(HcpmsPdfCellUtil.createCell(
					HcpmsParagraphUtil.createParagraphText(fontText,
							getValue(map, AuditConstants.processDate)),
					new int[] { 4, 4, 4, 60 }, Element.ALIGN_RIGHT,
					Rectangle.NO_BORDER));

			document.add(table);

		} catch (DocumentException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void setPdfWriter(PdfWriter pdf, Map<String, Object> map) {
	}

	public static String getValue(Map<String, Object> map, String name) {
		String str = "";
		if (name == null || "".equals(name))
			return str;
		Object obj = map.get(name);
		if (obj == null || "".equals(obj))
			return str;
		if (obj instanceof Date) {
			SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd");
			str = sft.format(obj);
		} else if (obj instanceof List) {

		} else if (obj instanceof Map) {

		} else {
			str = obj.toString();
		}
		return str;
	}
}
